var doListUrl = "/doList.do";
var orgName = "", orgCode = "", orgAddress = "",
    orgUserName = "", orgUserPhone = "", orgEmail = "",
    userName = "", tips = "", allFields = "";
/*页面初始化*/
$(document).ready(function () {
    setDioalog($("#org_dlg"));
    setDioalog($("#info_dialog"));
});

function checkValidate() {
    var bValid = true;
    orgName = $("#insert_name"),
        orgCode = $("#insert_orgCode"),
        orgAddress = $("#insert_address"),
        orgUserName = $("#insert_contact"),
        orgUserPhone = $("#insert_phone"),
        orgEmail = $("#insert_email"),
        userName = $("#insert_user");
    tips = $("#validate_tips");
    allFields = $([]).add(orgName).add(orgCode).add(orgAddress).add(orgUserName)
        .add(orgUserPhone).add(orgEmail).add(userName);

    allFields.removeClass("ui-state-error");
    bValid = bValid && checkLength(orgName, "机构名称", 1, 10);
    bValid = bValid && checkLength(orgCode, "组织机构代码", 1, 45);
    bValid = bValid && checkLength(orgAddress, "机构地址", 1, 128);
    bValid = bValid && checkLength(orgUserName, "联系人姓名", 1, 10);
    bValid = bValid && checkLength(orgUserPhone, "联系电话", 1, 11);
    bValid = bValid && checkLength(orgEmail, "联系邮箱", 1, 64);
    bValid = bValid && checkLength(userName, "用户名", 1, 20);

    bValid = bValid && checkRegexp(orgName, new RegExp("^[ A-Za-z0-9\\u0391-\\uFFE5_]{" + 1 + "," +10 + "}$"),"请输入汉字、英文字母、数字、汉字标点及下划线");
    bValid = bValid && checkRegexp(orgCode, /^\d+$/, "请输入数字");
    bValid = bValid && checkRegexp(orgAddress, new RegExp("^[ A-Za-z0-9\\u0391-\\uFFE5_]{" + 1 + "," +128 + "}$"), "请输入汉字、英文字母、数字、汉字标点及下划线");
    bValid = bValid && checkRegexp(orgUserName, new RegExp("^[ A-Za-z0-9\\u0391-\\uFFE5_]{" + 1 + "," +10 + "}$"), "请输入汉字、英文字母、数字、汉字标点及下划线");
    bValid = bValid && checkRegexp(orgUserPhone, /^(0|86|17951)?(13[0-9]|15[0-9]|17[678]|18[0-9]|14[57])[0-9]{8}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/, "输入的手机号码或者电话号码不正确");
    bValid = bValid && checkRegexp(orgEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/ , "邮箱不正确");
    bValid = bValid && checkRegexp(userName, new RegExp("^[ A-Za-z0-9\\u0391-\\uFFE5_]{" + 1 + "," +20 + "}$"), "请输入汉字、英文字母、数字、汉字标点及下划线");

    return bValid;
}
/*validate校验end*/

/* 搜索操作*/
function doSearch(url) {
    var jsonStr = {
        flg: 1,
        name: $('#js_name').val(),
        orgCode: $('#js_code').val(),
        orgId: "",
        page: ""
    };
    doList(jsonStr, url + doListUrl, $("#org_table"));
};

/*展示弹窗公用*/
function orgShowDialog(type,title,url){
    //$("#org_dlg").removeClass('hide');
    //$("#org_dlg").addClass("show");

    $("#org_dlg").dialog({
        dialogClass: "my-dialog",
        autoOpen: true,
        width: 850,
        modal: true,
        title:title,
        buttons: {
            保存: function () {
                if(checkValidate()){
                    storeALL(url)
                }
            },
            取消: function () {
                //$("#org_dlg").removeClass('show');
                //$("#org_dlg").addClass("hide");
                if(parseInt(type)!=3){
                    if(tips!="" || allFields!=""){
                        tips.text("").removeClass("ui-state-highlight");
                        allFields.removeClass( "ui-state-error").val("");
                    }
                }
                $(this).dialog('destroy');
                $(this).dialog("close");


            }
        },
        open:function(){
        if(parseInt(type)==3){
            $(".ui-dialog-buttonpane button").eq(0).hide(); //隐藏指定的button
        }
    },
        close: function() {
            tips.text("").removeClass("ui-state-highlight");
            allFields.removeClass( "ui-state-error").val("");
        }
    });
}

/*新增操作*/
function create(url) {
    var createUrl = url + "/create.do";
    orgShowDialog(1,"新增机构",createUrl);
    $(':input', '#org_fm')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    $('input').removeAttr("disabled");//去除input元素的disabled属性
};

/*修改操作*/
function modify(url, id) {
    var modifyUrl = url + "/modify.do?id=" + id;
    $.post(url + "/findId.do?id=" + id, function (result) {
        $("#org_dlg").html(result);
        $('input').removeAttr("disabled");//去除input元素的disabled属性
    });
    orgShowDialog(2,"编辑机构",modifyUrl);
};

/*保存数据共用部分*/
function storeALL(url) {
    $.ajax({
        cache: true,
        type: "POST",
        url: url,
        data: $('#org_fm').serialize(),
        async: false,
        error: function (request) {
            alert("Connection error");
        },
        success: function (result) {
            var data = $.parseJSON(result);
            if (data.ok) {
                $("#org_dlg").dialog("close");
                window.location.reload(false);
            } else {
                $("#info_msg").text(data.msg);
                $("#info_dialog").dialog({
                    dialogClass: "my-dialog",
                    autoOpen: true,
                    modal: true,
                    title: '提示信息',
                    buttons: {
                        确定: function () {
                            $(this).dialog("close");
                        }
                    }
                });
            }
        }
    });
};

/*删除操作*/
function delOrg(url, ids) {
    if (ids != null) {
        $("#info_msg").text("您确定要删除选择的机构吗？删除后无法恢复！");
        $("#info_dialog").dialog({
            dialogClass: "my-dialog",
            autoOpen: true,
            modal: true,
            title: '删除操作',
            buttons: {
                确定: function () {
                    $.post(url + '/delete.do?ids=' + ids, function (result) {
                        if (result.ok) {
                            $("#info_dialog").dialog("close");
                            window.location.reload(false);
                        }
                        else {
                            alert(result.msg);
                        }
                    }, 'json');
                },
                取消: function () {
                    $(this).dialog("close");
                }
            }
        });
    }
};

/*查看详情*/
function details(url, id) {
    $.post(url + "/findId.do?id=" + id, function (result) {
        $("#org_dlg").html(result);
        $('#org_dlg input').attr("disabled", "disabled")//将input元素设置为disabled
    });
    orgShowDialog(3,"机构详情","");
}